<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>close</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_000_001_512">&nbsp;</a>NAME</h4><blockquote>
close - close a file descriptor
</blockquote><h4><a name = "tag_000_001_513">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

#include &lt;<a href="unistd.h.html">unistd.h</a>&gt;

int close(int <i>fildes</i>);
</code>
</pre>
</blockquote><h4><a name = "tag_000_001_514">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>close()</i>
function will deallocate the file descriptor indicated by
<i>fildes</i>.
To deallocate means to
make the file descriptor available for return by subsequent calls to
<i><a href="open.html">open()</a></i>
or other functions that allocate file descriptors.
All outstanding record locks owned by the process on the file
associated with the file descriptor will be removed (that is,
unlocked).
<p>
If
<i>close()</i>
is interrupted by a signal that is to be caught, it
will return -1 with
<i>errno</i>
set to [EINTR] and the state of
<i>fildes</i>
is unspecified.
<p>
When all file descriptors associated with a pipe or FIFO special
file are closed, any data remaining in the pipe or FIFO
will be discarded.
<p>
When all file descriptors associated with an open file description have
been closed the open file description will be freed.
<p>
If the link count of the file is 0, when all file descriptors associated
with the file are closed, the space occupied by the file will be
freed and the file will no longer be accessible.
<p>
If a STREAMS-based <i>fildes</i> is closed and the calling process was
previously registered to receive a SIGPOLL signal
for events associated with that STREAM, the calling process will be
unregistered for events associated with the STREAM.  The last
<i>close()</i>
for a STREAM causes the STREAM associated with <i>fildes</i> to be dismantled.
If O_NONBLOCK is not set and there have been no signals posted for the STREAM,
and if there is data on the module's write queue,
<i>close()</i>
waits for an unspecified time (for each module and driver)
for any output to drain before dismantling the STREAM.  The time delay can be
changed via an I_SETCLTIME
<i><a href="ioctl.html">ioctl()</a></i>
request.  If the O_NONBLOCK flag is set, or if there
are any pending signals,
<i>close()</i>
does not wait for output to drain, and dismantles the STREAM immediately.
<p>
If the implementation supports STREAMS-based pipes,
and <i>fildes</i> is associated with one end of a pipe, the last
<i>close()</i>
causes a hangup to occur on the other end of the pipe.  In addition, if the
other end of the pipe has been named by
<i><a href="fattach.html">fattach()</a></i>,
then the last
<i>close()</i>
forces the named end to be detached by
<i><a href="fdetach.html">fdetach()</a></i>.
If the named end has no open file descriptors associated with it and gets
detached, the STREAM associated with that end is also dismantled.
<p>
If
<i>fildes</i>
refers to the master side of a pseudo-terminal, and this is the last
close, a SIGHUP signal is sent to the
process group, if any, for which the slave side of the pseudo-terminal is the
controlling terminal.  It is unspecified whether closing the master side of
the pseudo-terminal flushes all queued input and output.
<p>
If
<i>fildes</i>
refers to the slave side of a STREAMS-based pseudo-terminal,
a zero-length message may be sent to the master.
<p>
If the Asynchronous Input and Output option is supported:
<dl compact><dt> <dd>
When there is an outstanding cancelable asynchronous I/O operation against
<i>fildes</i>
when
<i>close()</i>
is called, that I/O operation may be canceled.
An I/O operation that is not canceled completes as if the
<i>close()</i>
operation had not yet occurred.
All operations that are not canceled complete as if the
<i>close()</i>
blocked until the operations completed.
The
<i>close()</i>
operation itself need not block awaiting such I/O completion.
Whether any I/O operation is cancelled,
and which I/O operation may be cancelled upon
<i>close()</i>,
is implementation-dependent.
</dl>
<p>
If the Mapped Files or Shared Memory Objects option is supported:
<dl compact><dt> <dd>
If a memory object remains referenced at the last close
(that is, a process has it mapped),
then the entire contents of the memory object persist
until the memory object becomes unreferenced.
If this is the last close of a memory object
and the close results in the memory object becoming unreferenced,
and the memory object has been unlinked, then
the memory object will be removed.
</dl>
</blockquote><h4><a name = "tag_000_001_515">&nbsp;</a>RETURN VALUE</h4><blockquote>
Upon successful completion, 0 is returned.  Otherwise, -1 is returned and
<i>errno</i>
is set to indicate the error.
</blockquote><h4><a name = "tag_000_001_516">&nbsp;</a>ERRORS</h4><blockquote>
The
<i>close()</i>
function will fail if:
<dl compact>

<dt>[EBADF]<dd>
The
<i>fildes</i>
argument is not a valid file descriptor.

<dt>[EINTR]<dd>
The
<i>close()</i>
function was interrupted by a signal.

</dl>
<p>
The
<i>close()</i>
function may fail if:
<dl compact>

<dt>[EIO]<dd>
An I/O error occurred while reading from or writing to the file system.

</dl>
</blockquote><h4><a name = "tag_000_001_517">&nbsp;</a>EXAMPLES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_001_518">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
An application that had used the <i>stdio</i> routine
<i><a href="fopen.html">fopen()</a></i>
to open a file should use the corresponding
<i><a href="fclose.html">fclose()</a></i>
routine rather than
<i>close()</i>.
</blockquote><h4><a name = "tag_000_001_519">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_000_001_520">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="fattach.html">fattach()</a></i>,
<i><a href="fclose.html">fclose()</a></i>,
<i><a href="fdetach.html">fdetach()</a></i>,
<i><a href="fopen.html">fopen()</a></i>,
<i><a href="ioctl.html">ioctl()</a></i>,
<i><a href="open.html">open()</a></i>,
<i><a href="unistd.h.html">&lt;unistd.h&gt;</a></i>,
<a href="STREAMS.html">STREAMS overview</a>.
</blockquote><h4>DERIVATION</h4><blockquote>
Derived from Issue 1 of the SVID.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>

